<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Account;
use App\Models\Yuanbao;
use Carbon\Carbon,DB;

class IndexController extends Controller
{
    public function createPromotion()
    {
        return view('createPromotion');
    }

    public function storePromotion(Request $request)
    {
        $res = Account::where('uid', $request->uid)->update(['nicecode' => $request->nicecode]);
        if ($res) {
            $data = [
                'status' => 0,
                'msg'    => '操作成功',
                'url'    => route('promotion.create')
            ];
        }
        else {
            $data = [
                'status' => 1,
                'msg'   => '操作失败',
            ];
        }

        return json_encode($data);
    }

    public function createRoomcard()
    {
        return view('createRoomcard');
    }

    public function storeRoomcard(Request $request)
    {
        $uid = $request->uid;
        $quantity = $request->quantity;

        $res = DB::connection('accounts_mj')->select('call sp_yuanbao_add(?, ?)', [$uid, $quantity]);

        if (is_object($res[0])) {
            $data = [
                'status' => 0,
                'msg' => '操作成功',
            ];
        }
        else {
            $data = [
                'status' => 1,
                'msg' => '操作失败',
            ];
        }

        return json_encode($data);
    }

    public function getGamers()
    {
        $gamers = Yuanbao::select('post_time','uid','quantity','quantity2')->orderBy('post_time', 'desc')->paginate(10);

        // dd($gamers->links()->toHtml());
        return view('gamers', compact('gamers'));
    }

    //1019611
    public function searchGamer(Request $request)
    {
        $uid = $request->uid;

        $gamers = Yuanbao::when($uid, function($query) use ($uid) {
                            return $query->where('uid', $uid);
                        })
                        ->orderBy('post_time', 'desc')->paginate(10);

        if ($request->ajax()) {
            $page = $gamers->appends(['uid' => $uid])->links()->toHtml();

            return json_encode(compact('gamers','page'));
        }
        else {
            $gamers->appends(['uid' => $uid]);

            return view('gamers', compact('gamers', 'uid'));
        }


    }

    public function summary()
    {
        $start = Carbon::now()->startOfDay()->toDateTimeString();
        $end = Carbon::now()->toDateTimeString();

        $gamers = Yuanbao::select('uid','post_time',DB::raw('sum(quantity2) as quantity_sum'))
                        ->where('type_id', 1000)
                        ->groupBy('post_time', 'uid')
                        ->orderBy('post_time', 'desc')
                        ->paginate(10);

        foreach ($gamers as $val) {
            $val->month_total =  Yuanbao::whereBetween('post_time', [$start, $end])->sum('quantity2');
        }

        return view('summary', compact('gamers'));
    }

    public function searchProxy(Request $request)
    {
        $uid = $request->uid;

        $start = Carbon::now()->startOfDay()->toDateTimeString();
        $end = Carbon::now()->toDateTimeString();

        $gamers = Yuanbao::select('uid','post_time',DB::raw('sum(quantity2) as quantity_sum'))
                        ->where('type_id', 1000)
                        ->when($uid, function($query) use($uid) {
                            return $query->where('uid', $uid);
                        })
                        ->groupBy('post_time', 'uid')
                        ->orderBy('post_time', 'desc')
                        ->paginate(10);

        foreach ($gamers as $val) {
            $val->month_total =  Yuanbao::whereBetween('post_time', [$start, $end])->sum('quantity2');
        }

        if ($request->ajax()) {
            $page = $gamers->appends(['uid' => $uid])->links()->toHtml();

            return json_encode(compact('gamers','page'));
        }
        else {
            $gamers->appends(['uid' => $uid]);

            return view('summary', compact('gamers', 'uid'));
        }
    }
}
